package duppentity.dto.recharge;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import utils.JSONObjectUtils;
import utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 将dupp返回的数据重新进行封装
 * Author: pengpeng@djcars.cn
 * Date: 2019/12/6 11:23
 */

public class BuRechargeDtoUtil {
    public static List<BuRechargeDto> getBuRechargeDtoList(JsonObject data) {
        List<BuRechargeDto> buRechargeDtoList = null;
        if (data != null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        buRechargeDtoList = new ArrayList<>();
                        for (Integer i = 0; i < list.size(); i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            BuRechargeDto buRechargeDto = convertBuRechargeDto(listOne, data);
                            buRechargeDtoList.add(buRechargeDto);
                        }
                    }
                }
            }
        }
        return buRechargeDtoList;
    }


    private static BuRechargeDto convertBuRechargeDto(JSONObject listOne, JsonObject data) {
        BuRechargeDto buRechargeDto = new BuRechargeDto();

        buRechargeDto.id = listOne.getLongValue("id");
        buRechargeDto.amount = Utils.getStringPrice(listOne.getIntValue("amount"));
        buRechargeDto.invalid = listOne.getIntValue("invalid");
        buRechargeDto.usedCount = listOne.getIntValue("usedCount");
        buRechargeDto.orderIndex = listOne.getIntValue("orderIndex");
        JSONArray buRechargeCouponIds = listOne.getJSONArray("buRechargeCouponIds");
        if (buRechargeCouponIds != null && buRechargeCouponIds.size() > 0) {
            StringBuilder buCouponName = new StringBuilder();
            for (Object rechargeCouponId : buRechargeCouponIds) {
                JSONObject buRechargeCoupon = JSONObjectUtils.get(data, "bu_recharge_coupon");
                JSONObject buCoupon = JSONObjectUtils.get(data, "bu_coupon");
                String couponId = JSONObjectUtils.getString(buRechargeCoupon, rechargeCouponId.toString(), "couponId");
                Integer num = JSONObjectUtils.getInteger(buRechargeCoupon, rechargeCouponId.toString(), "num");
                String name = JSONObjectUtils.getString(buCoupon, couponId, "name");
                buCouponName.append(name).append(" × ").append(num).append(",");
            }
            buRechargeDto.couponName = buCouponName.substring(0, buCouponName.length() - 1);
        }
        buRechargeDto.createTime = listOne.getLongValue("createTime");
        buRechargeDto.createBy = listOne.getLongValue("createBy");
        if (buRechargeDto.createBy > 0) {
            JSONObject sysAdmin = JSONObjectUtils.get(data, "sys_admin");
            buRechargeDto.name = JSONObjectUtils.getString(sysAdmin, buRechargeDto.createBy.toString(), "name");
        }
        return buRechargeDto;
    }
}

